/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.test.annotated;

import org.junit.Test;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.annotations.Attribute;
import org.sourceprojects.lycia.annotations.Base64;
import org.sourceprojects.lycia.annotations.TextBody;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.sourceprojects.lycia.fluent.FluentBuilder;
import org.sourceprojects.lycia.test.TestCaseUtilities;

import static org.sourceprojects.lycia.fluent.FluentBuilder.*;
import static org.junit.Assert.*;

public class Base64CombinationInjectionTestCase {

	private static final String XML_SIMPLE = TestCaseUtilities.XML_HEADER
			+ "<test><testcase-simple value=\"dGVzdA==\">dGVzdA==</testcase-simple></test>";

	private final Object contextObject = new Object();

	@Test
	public void testAnnotationExtendedInjectionNullUnaware()
			throws LyciaParsingException {
		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(nullAware(false),
				contextObject(contextObject)).parser(
				tag("testcase-simple", method("annotationExtendedInjection",
						this, String.class, String.class, byte[].class,
						byte[].class, String.class, byte[].class))).build();

		parser.parse(XML_SIMPLE);
	}

	@Test
	public void testAnnotationExtendedInjectionNullAware()
			throws LyciaParsingException {
		FluentBuilder<Object> builder = prepare();
		LyciaParser<Object> parser = builder.configure(
				contextObject(contextObject)).parser(
				tag("testcase-simple", method("annotationExtendedInjection",
						this, String.class, String.class, byte[].class,
						byte[].class, String.class, byte[].class))).build();

		parser.parse(XML_SIMPLE);
	}

	public void annotationExtendedInjection(
			@TextBody @Base64 String textBodyBase64, @Base64 String decoded,
			@TextBody @Base64 byte[] textBodyDecodedArray,
			@Base64 byte[] decodedArray,
			@Attribute("value") @Base64 String attributeBase64AsString,
			@Attribute("value") @Base64 byte[] attributeBase64) {

		assertNotNull(textBodyBase64);
		assertEquals("test", textBodyBase64);

		assertNotNull(decoded);
		assertEquals("test", decoded);

		assertNotNull(attributeBase64AsString);
		assertEquals("test", attributeBase64AsString);

		assertNotNull(decodedArray);
		assertNotNull(textBodyDecodedArray);
		assertNotNull(attributeBase64);

		assertArrayEquals(decodedArray, textBodyDecodedArray);
		assertArrayEquals(decodedArray, attributeBase64);
	}

}
